# -*- coding: utf-8 -*-

'''eof
name:申请人当前是否缴纳养老保险
code:Pension_PayStatus
tableName:
columnName:
groups:个人通用
dependencies:IC
type:常用指标
datasourceType:在线指标
description:
eof'''

import sys
import pandas as pd
import datetime
from dateutil.relativedelta import relativedelta
import json

reload(sys)
sys.setdefaultencoding('utf-8')

'''
申请人当前是否缴纳养老保险 
    提取个人征信报告-养老保险金缴存记录中的“本月缴费金额”和“缴费状态”字段进行判断
'''
null_type_list = ['', None, 'null', 'NULL', '/', 'None', 'Null',  ' ']
def FormatData(table_name):
    try:
        if IC in null_type_list:
            return [{}]
        if len(IC) == 0:
            return [{}]
        # icr是最外面一层表里面的
        icr = json.loads(IC[0]["icr"])[table_name]
        return icr if isinstance(icr, list) and len(icr) > 0 else [{}]
    except:
        return [{}]

def ConvertDataType(dataValue, dataType):
    returnData = None
    if dataType == 'float':
        try:
            returnData = float(dataValue) if float(dataValue) >= 0 else None
        except:
            returnData = None
    elif dataType == 'None':
        returnData = 0 if dataValue is None else dataValue
    return returnData

def  Pension_PayStatus():
    returnList = []
    try:
        last_month_list = [(datetime.datetime.now() - pd.tseries.offsets.DateOffset(months=i)).strftime('%Y-%m-%d') \
                           for i in [6, 0]]
        ICR_EID=FormatData("ICREndowmentInsuranceDeposit")
        if ICR_EID == [{}]:
            return u'缺失值'
        else:
            for ICR_EIDItem in ICR_EID:
                try:
                    Money = ConvertDataType(ICR_EIDItem.get('Money'), 'float')
                    State = ICR_EIDItem.get('State')
                    GetTime = Formate(ICR_EIDItem.get('GetTime'))
                    if Money not in null_type_list and State not in null_type_list and GetTime not in null_type_list:
                        if Money > 0 and State == u'参保缴费' and GetTime <= last_month_list[1] and GetTime > last_month_list[0]:
                            returnList.append(1)
                        else:
                            returnList.append(0)
                    else:
                        returnList.append(None)
                except:
                    returnList.append(None)
        returnListNotNull = filter(lambda x: x is not None, returnList)
        if len(returnListNotNull) == 0:
            return u'缺失值'
        elif sum(returnListNotNull) > 0:
            return u'是'
        elif sum(returnListNotNull) == 0:
            return u'否'
    except:
        return u'缺失值'


def Formate(time):
    import re
    import datetime
    r = re.compile(r'\D+')
    try:
        date = datetime.datetime.strptime(time, '%Y.%m.%d').strftime('%Y-%m-%d')
    except:
        try:
            date = datetime.datetime.strptime(time, '%Y-%m-%d').strftime('%Y-%m-%d')
        except:
            try:
                date = datetime.datetime.strptime(time, '%Y/%m/%d').strftime('%Y-%m-%d')
            except:
                try:
                    tmp = r.sub('', time)
                    date = datetime.datetime.strptime(tmp, '%Y%m%d').strftime('%Y-%m-%d')
                except:
                    date = None
    return date

result = Pension_PayStatus()

